package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ResourceAdapter
import com.cgbl.cg.adapter.ShoppingCarAdapter
import com.cgbl.cg.databinding.AppActivityShoppingcarBinding
import com.cgbl.cg.vm.mine.ShoppingCarViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.ShopCartBean

@Route(path = AppConstants.Router.Mine.A_MINE_SHOPPING_CAR)
class ShoppingCarActivity : GLBaseActivity<AppActivityShoppingcarBinding, ShoppingCarViewModel>() {

    lateinit var shoppingCarAdapter: ShoppingCarAdapter

    private var isEditMode = false

    override fun initComponents() {
        initTiterBar()
        initView()
        viewModel.refreshCart()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightTextView = findViewById<TextView>(R.id.tv_right_text)
        rightTextView.visibility = View.VISIBLE
        titleView.text = "我的购物车"
        backView.setOnClickListener { finish() }
        rightTextView.text = "编辑"

        rightTextView.setOnClickListener {
            isEditMode = !isEditMode
            rightTextView.text = if (isEditMode) "完成" else "编辑"
            binding.llEditBottom.visibility = if (isEditMode) View.VISIBLE else View.GONE
            binding.llNormalBottom.visibility = if (isEditMode) View.GONE else View.VISIBLE
        }
    }

    private fun initView() {
        shoppingCarAdapter = ShoppingCarAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(this@ShoppingCarActivity)
            adapter = shoppingCarAdapter
        }
        shoppingCarAdapter.setPickChange {
            viewModel.balance(it)
        }
        binding.ivAllCheck.setOnClickListener {
            binding.ivAllCheck.isSelected = !binding.ivAllCheck.isSelected
            if (binding.ivAllCheck.isSelected) {
                shoppingCarAdapter.selectAll()
            } else {
                shoppingCarAdapter.cancelSelectAll()
            }
        }

        SpanUtils.with(binding.tvTotalAmount).append("应付：")
            .setForegroundColor(ColorUtils.getColor(R.color.app_color_42)).setFontSize(12, true)
            .append("￥199.90").setForegroundColor(ColorUtils.getColor(R.color.app_color_ffc06e))
            .setFontSize(18, true).create()
    }

    override fun initViewObservable() {
        viewModel.uc.cartData.observe(this) {
            shoppingCarAdapter.setNewInstance(it as MutableList<ShopCartBean>?)
        }
        viewModel.uc.deleteSuccess.observe(this) {
            shoppingCarAdapter.cancelSelectAll()
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_shoppingcar

    override fun initVariableId(): Int = BR.viewModel
}